package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.VerifiedActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import retrofit2.Call;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class YhqAdapter extends BaseListAdapter<LinkedTreeMap> {

    public YhqAdapter(Context context, List<LinkedTreeMap> objects, String branch_id) {
        super(context, objects, branch_id);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_wb_yhq_item, parent, false);
                holder.money_wf = (TextView) convertView.findViewById(R.id.money_wf);
                holder.content_wf = (TextView) convertView.findViewById(R.id.content_wf);
                holder.lx_wf = (TextView) convertView.findViewById(R.id.lx_wf);
                holder.title_wf = (TextView) convertView.findViewById(R.id.title_wf);
                holder.time_wf = (TextView) convertView.findViewById(R.id.time_wf);
                holder.quantity_wf = (TextView) convertView.findViewById(R.id.quantity_wf);
                holder.ljlq_wf = (Button) convertView.findViewById(R.id.ljlq_wf);
                holder.hot = (ImageView) convertView.findViewById(R.id.hot);
                holder.wfLay = (LinearLayout) convertView.findViewById(R.id.wfLay);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
//            if (position >= 3){
//                holder.hot.setVisibility(View.GONE);
//            }
            holder.lx_wf.setText(mDataList.get(position).get("type_name") + "");
            holder.money_wf.setText(decimalFormat.format(mDataList.get(position).get("amount")));
            holder.content_wf.setText("消费满"+decimalFormat.format(mDataList.get(position).get("min_amount")) + "元可用");
            holder.title_wf.setText(mDataList.get(position).get("branch_name") + "");
            holder.time_wf.setText("领取截止时间："+ mDataList.get(position).get("get_end") + "");
            holder.quantity_wf.setText("每人限领："+ decimalFormat.format(mDataList.get(position).get("quantity")) + "张");
            holder.ljlq_wf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getMbUserInfo(position);
                }
            });
        }
        return convertView;
    }

    private static final class ViewHolder {
        TextView money_wf, content_wf, lx_wf, title_wf, time_wf, quantity_wf;
        Button ljlq_wf;
        ImageView hot;
        LinearLayout noDataRootLayout, wfLay;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

    private void getMbUserInfo(final int po) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(mContext) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("NAME")+"").equals("null")){
                        mContext.startActivity(new Intent(mContext, VerifiedActivity.class));
                    }else{
                        final ConfirmDialog confirmDialog = new ConfirmDialog(mContext, "确定领取吗？", 1);
                        confirmDialog.show();
                        confirmDialog.setCanceledOnTouchOutside(false);
                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmDialog.dismiss();
                                appCoupongetDetail(mDataList.get(po).get("p_key")+"");
                            }

                            @Override
                            public void doCancel() {
                                confirmDialog.dismiss();
                            }
                        });
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(mContext, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }
            }
        });
    }

    private void appCoupongetDetail(String P_KEY) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appCoupongetDetail(id, P_KEY,  BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(mContext) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(mContext, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
